package com.ruoyi.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.content.mapper.ArticlesMapper;
import com.ruoyi.content.mapper.UserInteractionsMapper;
import com.ruoyi.content.mapper.UserRelationshipsMapper;
import com.ruoyi.user.service.IXfsUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.user.mapper.XfsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 小番薯用户模块Service业务层处理
 *
 * @author chyn
 * @date 2025-03-14
 */
@Service
public class XfsUserServiceImpl implements IXfsUserService {
    @Autowired
    private XfsUserMapper sysUserMapper;
    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private UserInteractionsMapper userInteractionsMapper;
    @Autowired
    private UserRelationshipsMapper userRelationshipsMapper;

    /**
     * 查询小番薯用户模块
     *
     * @param userId 小番薯用户模块主键
     * @return 小番薯用户模块
     */
    @Override
    public SysUser selectSysUserByUserId(Long userId) {
        return sysUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询小番薯用户模块列表
     *
     * @param sysUser 小番薯用户模块
     * @return 小番薯用户模块
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增小番薯用户模块
     *
     * @param sysUser 小番薯用户模块
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser) {
        sysUser.setCreateTime(DateUtils.getNowDate());
        return sysUserMapper.insertSysUser(sysUser);
    }

    /**
     * 修改小番薯用户模块
     *
     * @param sysUser 小番薯用户模块
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser) {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 批量删除小番薯用户模块
     *
     * @param userIds 需要删除的小番薯用户模块主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserIds(Long[] userIds) {
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除小番薯用户模块信息
     *
     * @param userId 小番薯用户模块主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserId(Long userId) {
        return sysUserMapper.deleteSysUserByUserId(userId);
    }

    /**
     * 获取用户关注、粉丝、获赞、收藏 数
     *
     * @return
     */
    @Override
    @Transactional
    public Map<String, Integer> getUserStats(Long userId) {
        HashMap<String, Integer> map = new HashMap<>();
        Long currentUserId = SecurityUtils.getUserId();

        int followCount = userInteractionsMapper.getFollowCount(currentUserId);
        int fansCount = userInteractionsMapper.getFansCount(currentUserId);
        int likeCount = userRelationshipsMapper.getLikeCount(currentUserId);
        int collectCount = userRelationshipsMapper.getCollectCount(currentUserId);

        map.put("followCount", followCount);
        map.put("fansCount", fansCount);
        map.put("likeCount", likeCount);
        map.put("collectCount", collectCount);

        // todo 前端可能要改0 - 未关注 1 - 已关注
        if (userId != null && currentUserId != null && !currentUserId.equals(userId)) {
            int isFollowed = userInteractionsMapper.isFollowed(currentUserId, userId);
            map.put("isFollowed", isFollowed);
        }
        return map;
    }
}
